using SqlSugar;using TradeFlow.Server.Models;

namespace TradeFlow.Server.Service
{
    /// <summary>
    /// 首页服务类
    /// </summary>
    public class JxHomeService : IApplicationService
    {
        private readonly SugarRepository<JxBillHead> _billHeadRepository;
        private readonly ICurrentUser _currentUser;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="billHeadRepository"></param>
        /// <param name="currentUser"></param>
        public JxHomeService(SugarRepository<JxBillHead> billHeadRepository, ICurrentUser currentUser)
        {
            _billHeadRepository = billHeadRepository;
            _currentUser = currentUser;
        }

        /// <summary>
        /// 获取指定类型的单据数量
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public async Task<int> GetBillCountAsync(string type)
        {
            return await _billHeadRepository.AsQueryable()
                .Where(u => u.Creator == _currentUser.UserId && u.Type == type)
                .CountAsync();
        }

        /// <summary>
        /// 获取指定类型在指定日期的单据数量
        /// </summary>
        /// <param name="type"></param>
        /// <param name="date"></param>
        /// <returns></returns>
        public async Task<int> GetBillCountAsync(string type, DateTime date)
        {
            var startDate = date.Date;
            var endDate = date.Date.AddDays(1).AddTicks(-1);

            return await _billHeadRepository.AsQueryable()
                .Where(u => 
                    u.Creator == _currentUser.UserId && 
                    u.Type == type && 
                    u.BillDate >= startDate && 
                    u.BillDate <= endDate)
                .CountAsync();
        }

        /// <summary>
        /// 获取首页统计信息
        /// </summary>
        /// <returns></returns>
        public async Task<HomeInfo> GetHomeInfoAsync()
        {
            var homeInfo = new HomeInfo();
            var today = DateTime.Now;

            // 获取总统计
            homeInfo.Statistics = homeInfo.Statistics ?? new StatisticsInfo();
            homeInfo.Statistics.ImportCount = await GetBillCountAsync(BillType.Import);
            homeInfo.Statistics.ExportCount = await GetBillCountAsync(BillType.Export);
            
            // 注意：其他统计属性（如Today*和*ReturnCount）在StatisticsInfo类中不存在，暂时注释掉
            // homeInfo.Statistics.ImportReturnCount = await GetBillCountAsync(BillType.ImportReturn);
            // homeInfo.Statistics.ExportReturnCount = await GetBillCountAsync(BillType.ExportReturn);
            // homeInfo.Statistics.TodayImportCount = await GetBillCountAsync(BillType.Import, today);
            // homeInfo.Statistics.TodayImportReturnCount = await GetBillCountAsync(BillType.ImportReturn, today);
            // homeInfo.Statistics.TodayExportCount = await GetBillCountAsync(BillType.Export, today);
            // homeInfo.Statistics.TodayExportReturnCount = await GetBillCountAsync(BillType.ExportReturn, today);

            return homeInfo;
        }
    }
}